<!-- 同步日志 -->
<template>
  <div class="dashboard-editor-container">
    <el-row style="background: #fff; padding: 16px 16px 0;">
      <connsearch ref="connsearch" :formOptions="formOptions" @search="search" @reset="reset"/>
      <div class="tabclick">
        <!-- <div class="tabitem" @click="handleClick1()">
          <span>显示日统计趋势<i :class="tab1 ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span>
        </div> -->
        <div class="tabitem" @click="handleClick()">
          <span><i :class="tab ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"></i></span><!-- 显示综合统计 -->
        </div>
      </div>
      <el-row>
        <el-tabs type="border-card" v-if="tab" style="margin-top:16px;"  v-model="activeName" @tab-click="tabClick">
          <el-tab-pane label="显示日统计趋势" lazy name="day">
            <el-col :xs="24" :sm="24" :lg="24">
              <barchart :chartData="barchartData" v-if="this.activeName === 'day'" @barclick="barclick"/>
            </el-col>
          </el-tab-pane>
          <el-tab-pane label="终端用户统计分析" lazy name="user">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="24" :lg="24">
                <piechart  :chartData="pie1Data" v-if="this.activeName === 'user'" :title="'终端用户统计分析'" @pieclick="pieclick"/>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="同步规则统计分析" lazy name="rule">
            <el-col :xs="24" :sm="24" :lg="24">
              <piechart :chartData="pie2Data" v-if="this.activeName === 'rule'"  :title="'同步规则统计分析'" @pieclick="pieclick"/>
            </el-col>
          </el-tab-pane>
        </el-tabs>
      </el-row>
      <el-col :xs="{ span: 24 }" :sm="{ span: 24 }" :md="{ span: 24 }" :lg="{ span: 24 }" :xl="{ span: 24 }" style="padding-right: 8px;margin:16px 0;">
        <!-- <span class="title">事件日志列表</span> -->
        <el-table stripe border :data="tableData" style="width: 100%; margin-top: 15px">
          <el-table-column label="序号" width="80" type="index" align="left"></el-table-column>
          <el-table-column label="创建时间" prop="TransTime" width="160px" align="left"></el-table-column>
          <el-table-column label="规则名称" prop="RuleName" width="160px"  align="left"></el-table-column>
          <el-table-column label="终端用户" prop="Name" align="left"></el-table-column>
          <el-table-column label="备份状态" prop="Ret" align="left">
            <template slot-scope="scope">
              <span v-if="scope.row.Ret === 0" style="color:#409EFF">等待执行</span>
              <span v-if="scope.row.Ret === 1" style="color:#E6A23C">正在执行</span>
              <span v-if="scope.row.Ret === 2" style="color:#67C23A">执行成功</span>
              <span v-else style="color:#F56C6C">{{'执行失败'+ '（错误码：' + scope.row.Ret +'）'}}</span>
            </template>
          </el-table-column>
          <el-table-column label="备份开始" prop="StartTime" align="left"></el-table-column>
          <el-table-column label="备份结束" prop="EndTime" align="left"></el-table-column>
          <el-table-column label="备份耗时" prop="time" align="left">
            
          </el-table-column>
        </el-table>
        <el-pagination 
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" 
          :current-page=pageNum
          :page-size=pageSize
          background 
          layout="total, prev, pager, sizes, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import connsearch from "./components/connsearch.vue";
import barchart from "./components/barchart.vue";
import piechart from "./components/piechart.vue";
import { getNowDay, parseTime } from "@/utils/index.js";
import { connrequest } from "@/api/firstPage.js"
export default {
  components: { connsearch, barchart, piechart },
  data() {
    let that = this
    return {
      aaa:'',
      activeName: "day",
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tab: false,
      tab2: false,
      barchartData: {xAxis:[],series:[]},
      pie1Data: [],
      pie2Data: [],
      tableData:[],
      searchform:{},
      formOptions: [
        {
          col: 8,
          hidden: false,
          label: "日期", // label文字
          prop: "timeRange", // 字段名
          initValue: [], // 字段初始值,
          element: "el-date-picker", // 指定elementui组件
          type: "", // 日期选择类型,
        },
        {
          col: 8,
          hidden: false,
          label: "备份状态", // label文字
          prop: "Result", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: 0, // 字段初始值,
          placeholder: "请选择备份结果", // elementui组件属性
          options: [
            { label: '全部', value: 0 },
            { label: '等待执行', value: 1 },
            { label: '正在执行', value: 2 },
            { label: '执行成功', value: 3 },
            { label: '执行失败', value: 4 }
          ],
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val)
            },
          },
        },
        {
          col: 8,
          label: "终端用户", // label文字
          prop: "Name", // 字段名
          element: "el-select", // 指定elementui组件
          initValue: "", // 字段初始值,
          placeholder: "请选择终端用户", // elementui组件属性
          options: [],
          rules: [], // elementui组件属性
          events: {
            // elementui组件方法
            input(val) {
              // console.log(val);
            },
          },
        }
      ],
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    this.pageSize = this.$store.state.user.DefaultPageLength;
    let xTime = new Date().getTime()
    let endTime = new Date(new Date().setHours(0, 0, 0, 0))
    let startTime = endTime - ( this.$store.state.user.DefaultLogDays * 24 * 60 * 60 *1000);
    let arr = [ parseTime(startTime),'']
    this.formOptions.map( item => {
      if(item.label === '日期'){
        item.initValue = arr
      }
    })
    this.$set(this.searchform,'timeRange', arr)
    this.$set(this.searchform,'Result', 0)
    this.getData()
  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.GetEmployee()
    this.GetBackupLogSumGroupByDate()
  },
  methods: {
    // 获取所有数据
    getData(){
      this.GetBackupLog()
      // this.GetBackupLogSumGroupByUser()
      // this.GetBackupLogSumGroupByRule()
      // this.GetBackupLogSumGroupByDate()
    },
    tabClick(tab){
      // this.reset()
      this.getData()
      if(tab.name === 'day'){
        this.GetBackupLogSumGroupByDate()
      }else if(tab.name === 'user'){
        this.GetBackupLogSumGroupByUser();
      }else if(tab.name === 'rule'){
        this.GetBackupLogSumGroupByRule()
      }
    },
    barclick(val){
      this.pageNum = 1
      this.searchform.timeRange = [ `${val} 00:00:00`, `${val} 23:59:59` ]
      // let arr = [ `${val} 00:00:00`, `${val} 23:59:59` ]
      // this.formOptions = this.formOptions.map( item => {
      //   if(item.label === "日期"){
      //     item.initValue = arr
      //   }else if(item.label === "统计排名"){
      //     item.initValue = 10
      //   }else{
      //     item.initValue = ""
      //   }
      //   return item
      // })
      // this.$set(this.searchform, 'timeRange', arr)
      // this.$refs.connsearch.addInitValue()
      this.GetBackupLog()
    },
    pieclick(val,title,id){
      console.log(val,title);
      this.pageNum = 1
      if(title === '终端用户统计分析'){
        this.searchform.Name = val
        this.$refs.connsearch.searchform.Name = val
      }else if(title === '同步规则统计分析'){
        // this.searchform.EventID = id
        // this.$refs.connsearch.searchform.EventID = id
      }
      // if(title === '访问用户统计分析'){
      //   this.$set(this.searchform, 'Name', val)
      //   this.formOptions = this.formOptions.map( item => {
      //     if(item.label === "日期"){
      //       item.initValue = this.searchform.timeRange
      //     }else if(item.label === "统计排名"){
      //       item.initValue = 10
      //     }else if(item.label === "终端用户"){
      //       item.initValue = val
      //     }else{
      //       item.initValue = ""
      //     }
      //     return item
      //   })
      // }else{
      //   this.$set(this.searchform, 'Descp', val)
      //   this.$set(this.searchform, 'timeRange', this.searchform.timeRange)
      //   this.formOptions = this.formOptions.map( item => {
      //     if(item.label === "日期"){
      //       item.initValue = this.searchform.timeRange
      //     }else if(item.label === "统计排名"){
      //       item.initValue = 10
      //     }else if(item.label === "描述信息"){
      //       item.initValue = val
      //     }else{
      //       item.initValue = ""
      //     }
      //     return item
      //   })
      // }
      // this.$refs.connsearch.addInitValue()
      this.GetBackupLog()
    },
    // 获取终端用户
    async GetEmployee(){
      const res = await connrequest('GetEmployee')
      res.Data = res.Data.map(item => {
        item.label = item.Department +' / '+item.Name 
        item.value = item.Name
        return item
      })
      let arr = [{value: '', label: '全部'}]
      this.formOptions.map( item => {
        if(item.label === '终端用户'){
          item.options = [...arr,...res.Data]
        }
      })
    },
    // 获取事件日志列表
    async GetBackupLog(){
      const data = {
        Paging: {
          QueryPageNo: this.pageNum,
          QueryPageLength: this.pageSize
        },
        Filter: {
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          Name: this.searchform.Name || '',
          SumCount: 10,
          Result:this.searchform.Result
        }
      }
      const res = await connrequest('GetBackupLog', data)
      res.Data = res.Data.map(item => {
        item.time = item.EndTime - item.StartTime
        let d = Math.floor(item.time / 3600 / 24);
        let h = Math.floor((item.time  % 86400) / 3600);
        var m = Math.floor(((item.time % 86400) % 3600) / 60);
        var s = Math.round(item.time % 60);
        item.time = `${d < 1 ? '' : d}${d < 1 ? '' : '天'} ${h < 1? '' : '小时'} ${m}分钟 ${s}秒`;
        item.TransTime = parseTime(item.TransTime)
        item.StartTime = parseTime(item.StartTime)
        item.EndTime = parseTime(item.EndTime)
        return item
      })
      this.tableData = res.Data
      this.total = res.Paging.TotalCount
    },
    // 获取事件用户统计
    async GetBackupLogSumGroupByUser(){
      let arr = []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          Name: this.searchform.Name || '',
          SumCount: 10,
          Result:this.searchform.Result
        }
      }
      const res = await connrequest('GetBackupLogSumGroupByUser', data)
      res.Data.map( item =>{
        arr.push({name: item.Name, value: item.Count})
      })
      this.pie1Data = arr
    },
    // 获取事件域名统计
    async GetBackupLogSumGroupByRule(){
      let arr = []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          Name: this.searchform.Name || '',
          SumCount: 10,
          Result:this.searchform.Result
        }
      }
      const res = await connrequest('GetBackupLogSumGroupByRule', data)
      res.Data.map( item =>{
        arr.push({name: item.Name, id:item.ClassID, value: item.Count})
      })
      this.pie2Data = arr
    },
    // 获取事件日期统计
    async GetBackupLogSumGroupByDate(){
      let xAxis = []
      let series= []
      const data = {
        Filter:{
          StartTime: this.searchform.timeRange?.[0] || '',
          EndTime: this.searchform.timeRange?.[1] || '',
          Name: this.searchform.Name || '',
          SumCount: 10,
          Result:this.searchform.Result
        }
      }
      const res = await connrequest('GetBackupLogSumGroupByDate', data)
      res.Data.map( item =>{
        xAxis.push(item.Date)
        series.push(item.Count)
      })
      this.barchartData = {xAxis,series}
    },
    // 搜索
    search(val) {
      this.pageNum = 1
      this.searchform = {...val}
      this.getData();
      if(this.activeName === 'day'){
        this.GetBackupLogSumGroupByDate()
      }else if(this.activeName === 'user'){
        this.GetBackupLogSumGroupByUser();
      }else if(this.activeName === 'rule'){
        this.GetBackupLogSumGroupByRule()
      }
    },
    // 重置
    reset(){
      let xTime = new Date().getTime()
      let endTime = new Date(new Date().setHours(0, 0, 0, 0))
      let startTime = endTime - (window.sessionStorage.getItem('DefaultLogDays') * 1 * 3600 * 24 * 1000);
      let arr = [ parseTime(startTime), '']
      this.pageNum = 1
      this.formOptions = this.formOptions.map( item => {
        if(item.label === "日期"){
          item.initValue = arr
        }else if(item.label === "统计排名"){
          item.initValue = 10
        }else{
          item.initValue = ""
        }
        return item
      })
      // this.searchform = {}
      this.$set(this.searchform,'timeRange', arr)
      this.$set(this.searchform,'Result', 2)
      if(this.activeName === 'day'){
        this.GetBackupLogSumGroupByDate()
      }else if(this.activeName === 'user'){
        this.GetBackupLogSumGroupByUser();
      }else if(this.activeName === 'rule'){
        this.GetBackupLogSumGroupByRule()
      }
      this.$refs.connsearch.addInitValue()
      this.getData()
    },
    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.pageNum = 1
      this.GetBackupLog()
    },
    handleCurrentChange( pageNum ){
      this.pageNum = pageNum
      this.GetBackupLog()
    },
    handleClick() {
      this.tab = !this.tab
    },
    handleClick2() {
      this.tab2 = !this.tab2
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./components/css/conncss.scss";
</style>